function debounce(fn, interval) {
  // 最后一次执行完，规定interval时间后执行
  let timer = null;

  return function () {
    clearTimeout(interval);
    timer = setTimeout(() => {
      fn && fn.apply(this);
    }, interval);
  };
}

function throttle(fn, interval) {
  // 规定interval，执行一次
  let timer = null;

  return function () {
    // 如果有正在执行的，那就不管，让她执行完再说
    if (timer) return;
    timer = setTimeout(() => {
      fn && fn.apply(this);
      clearTimeout(interval);
    }, interval);
  };
}

function throttleV2(fn, interval) {
  let startTime = 0;

  return function () {
    const now = +new Date();
    if (now - startTime > interval) {
      fn && fn.apply(this);
      startTime = now;
    }
  };
}

function debounceV2(fn, interval) {
  let startTime;

  return function () {
    const now = +new Date();
    startTime = 0;
    if (now - startTime > interval) {
      fn && fn.apply(this);
      startTime = now;
    }
  };
}
